package com.tdam2013.grupo12.adapters;

import java.util.ArrayList;
import java.util.List;

import com.tdam2013.grupo12.R;
import com.tdam2013.grupo12.R.id;
import com.tdam2013.grupo12.R.layout;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.sax.StartElementListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

public class InfoContactoMailAdapter extends BaseAdapter{

	Activity context;
	ArrayList<String> emails;
	public InfoContactoMailAdapter(Context context) {
		this.context = (Activity)context;
		emails = new ArrayList<String>(0);
	}

	public void addTelefono(String email) {
		if (email != null) {
			emails.add(email);
		}
	}
	
	@Override
	public int getCount() {
		return emails.size();
	}
	@Override
	public Object getItem(int position) {
		return emails.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	private class MailHolder {
	       TextView txtMail;
	       ImageButton botonMail;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		MailHolder holder = null;
	       String email = (String) getItem(position);
	       final int pos = position;
	       LayoutInflater mInflater = (LayoutInflater) context
	               .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
	       if (convertView == null) {
	           convertView = mInflater.inflate(R.layout.activity_item_email_contacto, null);
	           holder = new MailHolder();
	           holder.txtMail = (TextView) convertView.findViewById(R.id.txtItemMail);
	           holder.botonMail = (ImageButton) convertView.findViewById(R.id.botonMail);
	           convertView.setTag(holder);
	       } else
	           holder = (MailHolder) convertView.getTag();

	       holder.txtMail.setText(email);
	       holder.txtMail.setOnClickListener(new OnClickListener() {
	    	   @Override
	           public void onClick(View v) {
	    		Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"+emails.get(pos)));
	    		context.startActivity(emailIntent);
	    		
	           }
	       });
	       return convertView;
	}


}
